//
//  WXJSViewController.m
//  ParentsCommunity
//
//  Created by tianlong on 2018/8/10.
//  Copyright © 2018年 XES. All rights reserved.
//

#import "WXJSViewController.h"
#import "WXJSWebViewController.h"

@interface WXJSViewController ()<UITextViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (unsafe_unretained, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (nonatomic, strong) NSMutableArray *textArray;
@end

@implementation WXJSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.textArray = [NSMutableArray array];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.textView.layer.borderWidth = 1;
    self.textView.layer.borderColor = [UIColor blackColor].CGColor;
    self.textView.text = @"";
    self.textView.delegate = self;
    
    //测试文件
    NSString *path = [[NSBundle mainBundle] pathForResource:@"mutual" ofType:@"html"];
    path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"];
    path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    path = @"activity.xueersi.com/topic/growth/invokeTest";
    NSURL *urlPath = nil;
    if(path) {
        urlPath = [NSURL fileURLWithPath:path];
    }
    self.textView.text = urlPath.absoluteString;
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(0, 0, 90, 44)];
    [backButton setImage:[UIImage imageNamed:@"homeworkpapertest_nav_back_icon_normal"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"homeworkpapertest_nav_back_icon_normal"] forState:UIControlStateHighlighted];
    [backButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [backButton setAutoresizesSubviews:YES];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    backButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [backButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, -5)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    [backButton addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationController.navigationBar.translucent = YES;

    self.navigationItem.title = @"This is entry";
}

- (void)btnClick {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)load:(id)sender {
    NSString *url = [self.textView.text stringByRemovingPercentEncoding];
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    if(url){
        WXJSWebViewController *webVC = [[WXJSWebViewController alloc] init];
        webVC.url = url;
        [self.navigationController pushViewController:webVC animated:YES];
    }
}

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length > 0) {
        [self.textArray insertObject:textView.text atIndex:0];
        if (self.textArray.count > 0) {
            [self.textArray removeLastObject];
        }
        [self.tableView reloadData];
    }
}

#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.textArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = self.textArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.textView.text = self.textArray[indexPath.row];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
